package adminLoginPack;

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServletRequest;

import InvestmentPojo.MailSettings;
  
/** 
 * Simple demonstration of using the javax.mail API. 
 *  
 * Run from the command line. Please edit the implementation 
 * to use correct email addresses and host name. 
 */  
public final class SendEMail  
{  
	 private static final String SMTP_HOST_NAME = "smtp.investmentgateways.com";
     private static final String SMTP_AUTH_USER = "siteadmin@investmentgateways.com";
     private static final String SMTP_AUTH_PWD  = "283936155dc8";
  
    /** 
     * Send a single email. 
     */  
    public boolean sendEmail( MailSettings settings, String toEmailAddr, String fromMailAddress, String subjectLine, HttpServletRequest request )  
    {
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.port", 25);
        props.put("mail.smtp.auth", "true");
        boolean statusFlag = false;
        try{
        Authenticator auth = new SMTPAuthenticator();
        Session mailSession = Session.getDefaultInstance(props, auth);
        
        
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);

        Multipart multipart = new MimeMultipart("alternative");

        BodyPart part1 = new MimeBodyPart();
        part1.setText("Checking to see what box this mail goes in ?");

        BodyPart part2 = new MimeBodyPart();
        String mailBody = settings.getBody();
        part2.setContent(mailBody, "text/html");

        multipart.addBodyPart(part1);
        multipart.addBodyPart(part2);

        
       
        message.setContent(multipart);
        message.setFrom(new InternetAddress("siteadmin@investmentgateways.com"));
        message.setSubject(subjectLine);
        message.addRecipient(Message.RecipientType.TO,
             new InternetAddress(toEmailAddr.trim()));
               transport.connect();
       
        transport.sendMessage(message,
            message.getRecipients(Message.RecipientType.TO));
        System.out.println("Send Message success fully.");
        transport.close();
        statusFlag = true;
    }catch(Exception e){
    	System.out.println("Sending Email Malfunction.............");
    	statusFlag = false;
    	e.printStackTrace();
    }
    return statusFlag;
    }
    private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
           String username = "siteadmin@investmentgateways.com";
           String password = "283936155dc8";
           return new PasswordAuthentication(username, password);
        }
    }
  
   }  